<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

use Unirest\Request AS API;

use Ptime3\Functions\Queue;

class UserController extends Controller
{
	function info(){

		if(empty($_SESSION["user_id"]) || empty($_SESSION["authorization"])){
			$state = urlencode("http://".env("APP_URL")."/user/info");
			$redirectUrl  = env("OAUTH_URL")."/auth?client_id=".env("CLIENT_ID")."&response_type=code&state=$state";
			return redirect($redirectUrl);
		}

		$header = [
			"host"          => "user",
			"authorization" => $_SESSION["authorization"]
		];

		$userResponse = API::get(env("PTIME_API")."/user/".$_SESSION["user_id"],$header);
		if($userResponse->code!=200 || !isset($userResponse->body->error) || $userResponse->body->error == true)
	        return "error";

		//return (array)$userResponse->body;

		$return = (array)$userResponse->body;
		return view('user/info',$return);
	}

	function logout(){
		if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') != false
			&& !empty($_SESSION["user_id"])){
			Queue::put("restapi",["put","user","user/".$_SESSION["user_id"],["unionid"=>""]]);
		}

		unset($_SESSION["user_id"]);
		unset($_SESSION["authorization"]);

		return view('user/logout',[]);
	}

    function verify(Request $request, $userId){

		$systemCat = "edu";
		$state     = $request->input("state");

		//认证
		$header = [
			"host"          => "campus",
			"authorization" => "Bearer ".env("ACCESS_TOKEN")
		];

		if(empty($request->input("force_oauth")))
			$userIntroResponse = API::post(env("PTIME_API")."/user/$userId/system_cat/edu/intro",$header);

		if(!empty($request->input("force_oauth")) ||
		  !isset($userIntroResponse->body->error) || $userIntroResponse->body->error == true){

			$state = urlencode("http://".env("APP_URL")."/user/$userId/verify?state=".urlencode($state));
			$redirectUrl  = env("CAMPUS_OAUTH_URL")."/auth?state=$state&user_id=$userId&system_cat=$systemCat";
			return redirect($redirectUrl);
		}

		//处理认证结果
		$userData    = $userIntroResponse->body->result->data;
		$userUpdateData = [
			"school_id"   => $userData->xh,
			"name"        => $userData->xm,
			"description" => $userData->bj,
			"sex"         => $userData->xb=="男"?1:0,
			"is_verified"  => 1
		];
		$header = [
			"host"          => "user",
			"authorization" => "Bearer ".env("ACCESS_TOKEN")
		];
		$body = \Unirest\Request\Body::form($userUpdateData);
		$userUpdateResponse = API::put(env("PTIME_API")."/user/$userId",$header,$body);
		if(!isset($userUpdateResponse->body->error))
			$return = ["error"=>true,"result"=>"","message"=>"遭遇未知错误"];
		else
			$return = (array)$userUpdateResponse->body;

		//构造返回数据
		$return["retry"] = "http://".env("APP_URL")."/user/$userId/verify?state=".urlencode($state)."&force_oauth=1";

		if($state != "")
			$state .= (strpos("?", $state)===false?"?":"&")."verify_result=".$return["error"];

		$return["state"] = $state;
		return view('user/verify',$return);
    }

}
